package sz.szsmk.citizencard.mvp.presenter;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.jetbrains.annotations.Nullable;

import javax.inject.Inject;

import retrofit.http.Body;
import retrofit.http.POST;
import rx.Observable;
import rx.functions.Action1;
import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.gover.FunctionItem;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.AccessTokenRequest;
import sz.szsmk.citizencard.model.network.bean.request.AccountExistReq;
import sz.szsmk.citizencard.model.network.bean.request.ChangeUserFuncReq;
import sz.szsmk.citizencard.model.network.bean.request.GetOrderOpenid;
import sz.szsmk.citizencard.model.network.bean.request.IBaseRequest;
import sz.szsmk.citizencard.model.network.bean.request.UserBehaviorBrowseReq;
import sz.szsmk.citizencard.model.network.bean.request.VerifyBindNgUserReq;
import sz.szsmk.citizencard.model.network.bean.response.AccountExistRes;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.CloudFlashRes;
import sz.szsmk.citizencard.model.network.bean.response.GetOrderOpenidRes;
import sz.szsmk.citizencard.model.network.bean.response.QueryAllFunctionNewRes;
import sz.szsmk.citizencard.model.network.bean.response.SeleciUserAuthRes;
import sz.szsmk.citizencard.model.network.bean.response.VerifyBindNgUserRes;
import sz.szsmk.citizencard.mvp.contract.IGoverContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.retrofitrxjava.newversion.ApiCallback;
import sz.szsmk.citizencard.retrofitrxjava.newversion.HttpDateSource;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/12/21 16:10
 * description:
 */
public class GoverPresenter extends IBasePresenter
        implements IGoverContact.IGoverPresenter<IGoverContact.IGoverView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public GoverPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;
        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }


    @Override
    public void getViewData() {
        IBaseRequest baseRequest = new IBaseRequest();
        String json = new Gson().toJson(baseRequest);
        Log.e("json",json);
        mNetworkDateSource.queryAllFunctionNew(baseRequest, new SubscribeResult<QueryAllFunctionNewRes>() {
            @Override
            protected void onOk(QueryAllFunctionNewRes response) {
                String json = new Gson().toJson(response);
/**
 * 这是手动添加的图标，用来测试拍卡 教育E卡通的
                QueryAllFunctionNewRes.ClassListBean.FunctionListBean beanEKATONG = new QueryAllFunctionNewRes.ClassListBean.FunctionListBean();
                QueryAllFunctionNewRes.ClassListBean.FunctionListBean beanPKACHONGZHI = new QueryAllFunctionNewRes.ClassListBean.FunctionListBean();

                beanEKATONG.setFunction_id(110);
                beanEKATONG.setRank(24);
                beanEKATONG.setFunction_name("教育卡增效");
                beanEKATONG.setIcon_url("http://zhszyfb2020.szsmk.com/images/firsthotsales/20201126/ef36deee-b077-40e0-96b3-2965a7d880b4.png");
                beanEKATONG.setResource_page_id(110);
                beanEKATONG.setLink_url("EKATONG");
                beanEKATONG.setSys_label("EKATONG");
                beanEKATONG.setNeed_header(0);
                beanEKATONG.setModularId(0);

                beanPKACHONGZHI.setFunction_id(111);
                beanPKACHONGZHI.setRank(25);
                beanPKACHONGZHI.setFunction_name("拍卡充值");
                beanPKACHONGZHI.setIcon_url("http://zhszyfb2020.szsmk.com/images/firsthotsales/20201126/6af0aaf8-3069-49cb-ba07-5fa1d06da0d7.png");
                beanPKACHONGZHI.setResource_page_id(111);
                beanPKACHONGZHI.setLink_url("PKACHONGZHI");
                beanPKACHONGZHI.setSys_label("PKACHONGZHI");
                beanPKACHONGZHI.setNeed_header(0);
                beanPKACHONGZHI.setModularId(0);

                response.getClass_list().get(3).getFunction_list().add(beanEKATONG);
                response.getClass_list().get(3).getFunction_list().add(beanPKACHONGZHI);
**/
                ((IGoverContact.IGoverView) mView).getViewDataSucc(response);

            }

            @Override
            protected void onFailure(QueryAllFunctionNewRes response) {
                ((IGoverContact.IGoverView) mView).getViewDataFail(response);
            }

            @Override
            protected void onNext(QueryAllFunctionNewRes response) {
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void changeUserFunc(String idList) {
        ChangeUserFuncReq changeUserFuncReq = new ChangeUserFuncReq();
        changeUserFuncReq.setUser_function_list(idList);
        String json = new Gson().toJson(changeUserFuncReq);
        L.e(Constant.TAG, "添加的" + json);
        mNetworkDateSource.changeUserFunc(changeUserFuncReq, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((IGoverContact.IGoverView) mView).changeUserFuncSucc();
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                ((IGoverContact.IGoverView) mView).changeUserFuncFail(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void selectUserAuth(final String type, FunctionItem data) {
        AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
        mNetworkDateSource.selectUserAuth(accessTokenRequest, new SubscribeResult<SeleciUserAuthRes>() {
            @Override
            protected void onOk(SeleciUserAuthRes response) {
                ((IGoverContact.IGoverView) mView).selectUserAuthSucc(response,type,data);
            }

            @Override
            protected void onFailure(SeleciUserAuthRes response) {
                super.onFailure(response);
                Toast.makeText(DaggerApplication.getIns(), response.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void checkSuccessOrderStatus() {
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        OkHttpUtils.post()
                .url(Constant.BASE_URL + "app/supply/CheckSuccessOrderStatusReq")
                .addParams("access_token", access_token)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Request request, Exception e) {

                    }

                    @Override
                    public void onResponse(String response) {
                        CloudFlashRes cloudFlashRes = new Gson().fromJson(response, CloudFlashRes.class);
                        if (cloudFlashRes != null) {
                            if (mView != null) {
                                String code = cloudFlashRes.getCode();
                                if (code.equals("000000")) {
                                    String orderStatus = cloudFlashRes.getOrderStatus();
                                    if (orderStatus != null) {
                                        ACache.get(DaggerApplication.getIns()).put("orderStatus", orderStatus);
                                    }
                                    ACache.get(DaggerApplication.getIns()).put("orderStatus", orderStatus);
                                    ((IGoverContact.IGoverView) mView).checkSuccessOrderStatusSucc(orderStatus);
                                } else {
                                    ((IGoverContact.IGoverView) mView).checkSuccessOrderStatusFail(cloudFlashRes);
                                }
                            }

                        }
                    }
                });
    }

    @Override
    public void getGetOrderId(final String url, final String type, FunctionItem data) {
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        GetOrderOpenid getOrderOpenid = new GetOrderOpenid();
        getOrderOpenid.setAccess_token(access_token);
        getOrderOpenid.setTag("GUIHUAFEN");
        String json = new Gson().toJson(getOrderOpenid);
        L.e(Constant.TAG, "GoverPresentergetOrderOpenid----->" + json);
        mNetworkDateSource.getOrderOpenid(getOrderOpenid, new SubscribeResult<GetOrderOpenidRes>() {
            @Override
            protected void onOk(GetOrderOpenidRes response) {
                String toJson = new Gson().toJson(response);
                Constant.EXTENT = response.getExtend();
                L.e(Constant.TAG, "GoverPresentergetOrderOpenid----->" + response.getExtend());
                ((IGoverContact.IGoverView) mView).getGetOrderIdSucc(response,type,url,data);
            }

            @Override
            protected void onNext(GetOrderOpenidRes response) {
                super.onNext(response);
                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "-------------" + toJson);
            }

            @Override
            protected void onFailure(GetOrderOpenidRes response) {
                super.onFailure(response);
                String toJson = new Gson().toJson(response);
                if (response.getCode().equals("0000")) {
                    ACache.get(DaggerApplication.getIns()).remove("mextend");
                    ACache.get(DaggerApplication.getIns()).put("mextend", response.getExtend());
                }
                L.e(Constant.TAG, "-------------" + toJson);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }

    @Override
    public void getGetOrderIdForNGBOSS(String url, String tyhm, FunctionItem data) {
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        GetOrderOpenid getOrderOpenid = new GetOrderOpenid();
        getOrderOpenid.setAccess_token(access_token);
        getOrderOpenid.setTag("NGBOSS");
        String json = new Gson().toJson(getOrderOpenid);
        L.e(Constant.TAG, "getOrderOpenid----->" + json);
        mNetworkDateSource.getOrderOpenid(getOrderOpenid, new SubscribeResult<GetOrderOpenidRes>() {
            @Override
            protected void onOk(GetOrderOpenidRes response) {
                ((IGoverContact.IGoverView) mView).getGetOrderIdForNGBOSSResult(response,tyhm,url,data);
            }

            @Override
            protected void onNext(GetOrderOpenidRes response) {
                super.onNext(response);
            }

            @Override
            protected void onFailure(GetOrderOpenidRes response) {
                super.onFailure(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }

    @Override
    public void getGetOrderIdForGHF(String url, String tyhm) {
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        GetOrderOpenid getOrderOpenid = new GetOrderOpenid();
        getOrderOpenid.setAccess_token(access_token);
        getOrderOpenid.setTag("GUIHUAFEN");
        String json = new Gson().toJson(getOrderOpenid);
        L.e(Constant.TAG, "getOrderOpenid----->" + json);
        mNetworkDateSource.getOrderOpenid(getOrderOpenid, new SubscribeResult<GetOrderOpenidRes>() {
            @Override
            protected void onOk(GetOrderOpenidRes response) {
                Constant.EXTENT_GHF = response.getExtend();
                ((IGoverContact.IGoverView) mView).getGetOrderIdForGHFResult(response,tyhm,url);
            }

            @Override
            protected void onNext(GetOrderOpenidRes response) {
                super.onNext(response);
            }

            @Override
            protected void onFailure(GetOrderOpenidRes response) {
                super.onFailure(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }

    @Override
    public void verifyBindNgUser() {
        // 判断是否绑定NG用户
        VerifyBindNgUserReq verifyBindNgUserReq = new VerifyBindNgUserReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        verifyBindNgUserReq.setAccess_token(access_token);

        mNetworkDateSource.verifyBindNgUser(verifyBindNgUserReq, new Action1<VerifyBindNgUserRes>() {
            @Override
            public void call(VerifyBindNgUserRes verifyBindNgUserRes) {
                ((IGoverContact.IGoverView) mView).verifyBindNgUserSucc(verifyBindNgUserRes);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }

    //用户确认授权接口
    @Override
    public void toBindNgUser() {
        VerifyBindNgUserReq verifyBindNgUserReq = new VerifyBindNgUserReq();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        verifyBindNgUserReq.setAccess_token(access_token);

        mNetworkDateSource.toBindNgUser(verifyBindNgUserReq, new Action1<VerifyBindNgUserRes>() {
            @Override
            public void call(VerifyBindNgUserRes verifyBindNgUserRes) {
                if(verifyBindNgUserRes.getCode().equals("000000")){
                    ((IGoverContact.IGoverView) mView).toBindNgUserSucc(verifyBindNgUserRes);
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }


    @Override
    public void userBehaviorBrowse(int id) {
        UserBehaviorBrowseReq userBehaviorBrowseReq = new UserBehaviorBrowseReq();
        userBehaviorBrowseReq.setModularId(id);
        userBehaviorBrowseReq.setAccessToken(ACache.get(DaggerApplication.getIns()).getAsString("access_token"));

        mNetworkDateSource.userBehaviorBrowse(userBehaviorBrowseReq, new Action1<BaseBean>() {
            @Override
            public void call(BaseBean baseBean) {

                L.e(Constant.TAG, "ok----->" );
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
            }
        });
    }
    //校验桂花分账号是否存在
    public void accountExist(String level,String idCard,String phone,String realName) {
        AccountExistReq accountExistReq = new AccountExistReq();
        accountExistReq.setAuthenticationLevel(level);
        accountExistReq.setIdCard(idCard);
        accountExistReq.setPhoneNum(phone);
        accountExistReq.setRealName(realName);
        mNetworkDateSource.queryAccountExist(accountExistReq, new Action1<AccountExistRes>() {
            @Override
            public void call(AccountExistRes accountExistRes) {

            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

}
